package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.HarmlessDao;
import com.jiantong.work.model.Harmless;
import com.jiantong.work.service.HarmlessReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/24.
 *
 */
@Slf4j
@Service
@RpcProvider
public class HarmlessReadServiceImpl implements HarmlessReadService {

    private final HarmlessDao harmlessDao;

    @Autowired
    public HarmlessReadServiceImpl(HarmlessDao harmlessDao) {
        this.harmlessDao = harmlessDao;
    }

    @Override
    public Response<Harmless> findById(Long id) {
        try{
            Harmless harmless = harmlessDao.findById(id);
            if(harmless == null){
                log.error("harmless(id={}) is not found", id);
                return Response.fail("harmless.not.found");
            }
            return Response.ok(harmless);
        }catch (Exception e){
            log.error("failed to find harmless(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("harmless.find.fail");
        }
    }

    @Override
    public Response<Map<String , Object>> findStatusById(Long id) {
        try{
            Integer status = harmlessDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("harmless status (id={}) is not found", id);
                return Response.fail("harmless.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("harmless stats (id={}) is not found", id);
            return Response.fail("harmless.status.not.found");
        }
    }

    @Override
    public Response<Map<String, Integer>> findStatusAndTreatment(Long id) {
        try {
            Map<String , Integer> map = harmlessDao.findStatusAndTreatment(id);
            if(map == null){
                log.error("failed.to.find.status.and.treatment id = {}",id);
                return Response.fail("find.status.and.treatment failed");
            }
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed.to.find.status.and.treatment id = {}",id);
            return Response.fail("find.status.and.treatment failed");
        }
    }

    @Override
    public Response<List<Map<String, Integer>>> findSumIsLocal(Date start, Date end, Integer animalType) {
        try {
            Map<String ,Object> map = Maps.newHashMap();
            if(start != null) map.put("start",start);
            if(end != null) map.put("end",end);
            if(animalType != null) map.put("animalType",animalType);
            map.put("status",2);
            map.put("reportType",2);
            //todo 放入本市的申报点 和屠宰场
            return Response.ok(harmlessDao.findSumIsLocal(map));
        }catch (Exception e){
            log.error("failed to sum harmless is local, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("is local harmless.is local.fail");
        }
    }

    @Override
    public Response<List<Map<String, Integer>>> findSumReportType(Date start, Date end, Integer animalType) {
        try {
            Map<String ,Object> map = Maps.newHashMap();
            if(start != null) map.put("start",start);
            if(end != null) map.put("end",end);
            if(animalType != null) map.put("animalType",animalType);
            map.put("status",2);
            //todo 放入本市的申报点 和屠宰场
            return Response.ok(harmlessDao.findSumReportType(map));
        }catch (Exception e){
            log.error("failed to sum harmless ReportType, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("is local harmless.ReportType.fail");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findAWeekSumIsLocal(Integer animalType) {
        try {
            Map<String ,Object> map = Maps.newHashMap();
            if(animalType != null) map.put("animalType",animalType);
            map.put("status",2);
            map.put("reportType",2);
            //todo 放入本市的申报点 和屠宰场
            return Response.ok(harmlessDao.findAWeekSumIsLocal(map));
        }catch (Exception e){
            log.error("failed to sum harmless IsLocal a week, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("is local harmless.IsLocal.a week.fail");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findAWeekSumReportType(Integer animalType) {
        try {
            Map<String ,Object> map = Maps.newHashMap();
            if(animalType != null) map.put("animalType",animalType);
            map.put("status",2);
            //todo 放入本市的申报点 和屠宰场
            return Response.ok(harmlessDao.findAWeekSumReportType(map));
        }catch (Exception e){
            log.error("failed to sum harmless ReportType a week, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("is local harmless.ReportType.a week.fail");
        }
    }

    @Override
    public Response<Paging<Harmless>> pagingHarmless(Integer pageNo, Integer pageSize,
                                                     List<Integer> statuses , Long breedFactoryId ,
                                                     Long butcherFactoryId) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);

            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();
            List<Long> butcherFactoryIds = new ArrayList<>();

            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds",breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(butcherFactoryId != null){
                butcherFactoryIds.add(butcherFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                butcherFactoryIds = loginUser.getButcherFactoryIds();
            }
            criteria.put("butcherFactoryIds",butcherFactoryIds);

            if(statuses != null) {
                criteria.put("statuses",statuses);
            }

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(harmlessDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("harmless.paging.fail");
        }
    }

    @Override
    public Response<Paging<Harmless>> farmPagingHarmless(Integer pageNo, Integer pageSize, List<Integer> statuses , Long breedFactoryId) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId",breedFactoryId);

            criteria.put("reportType", DocumentsType.QUARANTINE_ORIGIN_AREA.value());

            if(statuses!= null){
                criteria.put("statuses",statuses);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(harmlessDao.farmPagingHarmless(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging  farm harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("farm harmless.paging.fail");
        }
    }

    @Override
    public Response<Paging<Harmless>> butcheryPagingHarmless(Integer pageNo, Integer pageSize, List<Integer> statuses,Long butcherFactoryId) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("butcherFactoryId",butcherFactoryId);
            if(statuses!= null){
                criteria.put("statuses",statuses);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(harmlessDao.butcheryPagingHarmless(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.paging.fail");
        }
    }


    @Override
    public Response<Paging<Harmless>> pagingHarmlessAll(Integer pageNo, Integer pageSize, List<Integer> statuses,List<Integer> reportTypes) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            if(statuses!= null){
                criteria.put("statuses",statuses);
            }
            if(reportTypes != null){
                criteria.put("reportTypes",reportTypes);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(harmlessDao.pagingHarmlessAll(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.paging.fail");
        }
    }

    /**
     *通用条件查询
     * @param pageNo
     * @param pageSize
     * @param criteria
     * @return
     */
    @Override
    public Response<Paging<Harmless>> pagingCommon(Integer pageNo, Integer pageSize, Map criteria) {
        try{
        PageInfo pageInfo = new PageInfo(pageNo,pageSize);
        return Response.ok(harmlessDao.pagingCommon(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.paging.fail");
        }
    }

    @Override
    public Response<List<String>> reportType() {
        try{
            return Response.ok(harmlessDao.reportType());
        }catch (Exception e){
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.fail");
        }
    }
    @Override
    public Response<List<Map>> harmlessranking(Map criteria){
        try {
            return Response.ok(harmlessDao.harmlessranking(criteria));
        }catch (Exception e){
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.fail");
        }
    }
    @Override
    public Response<List<Map>> harmlesspic(Map criteria){
        try {
            return Response.ok(harmlessDao.harmlesspic(criteria));
        } catch (Exception e) {
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.fail");
        }

    }

    @Override
    public Response<List<Harmless>> selectByFactoryIds(Map criteria) {
        try {
            return Response.ok(harmlessDao.selectByFacotryIds(criteria));
        } catch (Exception e) {
            log.error("failed to paging  butchery harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("butchery harmless.fail");
        }
    }

    /**
     * 溯源所用
     *
     * @param markNumber 耳标号
     * @param reportId   单据ID
     * @return Harmless
     */
    @Override
    public Response<Harmless> findHarmlessByMarkNumberAndReportId(String markNumber, Long reportId) {
        try {
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("markNumber",markNumber);
            criteria.put("reportId",reportId);
            return Response.ok(harmlessDao.findHarmlessByMarkNumberAndReportId(criteria));
        } catch (Exception e) {
            log.error("failed to find harmless, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("harmless.fail");
        }
    }
}
